package org.me;

import java.util.ArrayList;
import java.util.Collection;

import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.Presence.Mode;
import org.jivesoftware.smack.util.StringUtils;
import org.me.utils.Koneksi;
import org.me.utils.MyService;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class MyProfile extends Activity implements AdapterView.OnItemSelectedListener {
	
	private static final String[] presence_items ={"Available", "Away", "Busy"};
	EditText personal_message;
	Presence presence;
	String selected;
	ArrayList<String> as;
    int pos;
    String pres;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.profile);
		
		String userName = StringUtils.parseName(Koneksi.getInstance().getConnection().getUser());
//		ContactList.mHandler.removeCallbacks(ContactList.Refresh);
		
		Typeface font = Typeface.createFromAsset(getAssets(), "fonts/tt0142m_.ttf");
		
		TextView nameprofile = (TextView) findViewById(R.id.nameprofile);
		nameprofile.setTypeface(font);
		nameprofile.setText(userName);
		
		TextView statusprofile = (TextView) findViewById(R.id.statusprofile);
		statusprofile.setTypeface(font);
		
		TextView text1 = (TextView) findViewById(R.id.text_display_name);
		text1.setTypeface(font);
		TextView text2 = (TextView) findViewById(R.id.text_personal_message);
		text2.setTypeface(font);
		TextView text3 = (TextView) findViewById(R.id.text_status_spinner);
		text3.setTypeface(font);
		
//		as = Koneksi.getInstance().getContact();
//        pos = Koneksi.getInstance().getPosition();
		String username = StringUtils.parseName(Koneksi.getInstance().getConnection().getUser());
		String address = StringUtils.parseBareAddress(Koneksi.getInstance().getConnection().getUser());
        Roster ros = Koneksi.getInstance().getConnection().getRoster();
        
        
//        Collection<RosterEntry> entries = ros.getEntries();
//        Presence presence;
//        for(RosterEntry entry : entries) {
//            presence = ros.getPresence(entry.getUser());
//
//            System.out.println(entry.getUser());
//            System.out.println(presence.getType().name());
//            if (presence.getMode() != null) {
//            	System.out.println(presence.getMode().name());
//            }
//            if (presence.getStatus() != null) {
//            	System.out.println(presence.getStatus());
//			}
//        }
		
		EditText name = (EditText) findViewById(R.id.display_name);
		name.setText(username);
		
		personal_message = (EditText) findViewById(R.id.personal_message);
		personal_message.setText(Koneksi.getStatus_message());
		
		
		Spinner status_spinner = (Spinner)this.findViewById(R.id.status_spinner);
		status_spinner.setOnItemSelectedListener(this);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(  
		        this,
		        android.R.layout.simple_spinner_item,
		        MyProfile.presence_items);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
		status_spinner.setAdapter(adapter);
		
		Button save = (Button) findViewById(R.id.saveprofile);
		save.setTypeface(font);
		save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Koneksi.setStatus_message(personal_message.getText().toString());
				presence.setStatus(Koneksi.getStatus_message());
				Koneksi.getInstance().getConnection().sendPacket(presence);
			}
		});
		
		Button signout = (Button) findViewById(R.id.signout);
		signout.setTypeface(font);
		signout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ContactList.mHandler.removeCallbacks(ContactList.Refresh);
				Koneksi.getInstance().destroy();
				Intent chatIntent = new Intent(v.getContext(), Chat.class);
				chatIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(chatIntent);
				Intent service = new Intent(MyProfile.this, MyService.class);
				MyProfile.this.stopService(service);
			}
		});
		
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Koneksi.setStatus_message(personal_message.getText().toString());
		presence.setStatus(Koneksi.getStatus_message());
		Koneksi.getInstance().getConnection().sendPacket(presence);
		ContactList.mHandler.post(ContactList.Refresh);
		
		Intent myIntent = new Intent(this, TabActivity.class);
		startActivity(myIntent);
		
	}
	
	
	public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		selected = presence_items[pos];
//		System.out.println(selected);
//		Koneksi.status_presence = selected;
		
		presence = new Presence(Presence.Type.available);
		
		switch (pos) {
		case 0:
			presence.setMode(Mode.available);
			break;
		case 1:
			presence.setMode(Mode.away);
			break;
		case 2:
			presence.setMode(Mode.dnd);
			break;

		default:
			presence.setMode(Mode.available);
			break;
		}
	}

	public void onNothingSelected(AdapterView<?> arg0) {
		
	}
	
}
